// 发请求获取数据
async function loadData() {
    const res = await axios({
        url: '/dashboard'
    })
    console.log('发请求拿到的结果', res)

    initOverView(res.data.overview)
    // 初始化折线图
    initLine(res.data.year)
    // 初始化饼图
    initPie(res.data.salaryData)
    // 初始化柱状图
    initBar(res.data.groupData)
    // 调用地图
    initMapChart(res.data.provinceData)
    initTwoPie(res.data.salaryData)
}

// 渲染概览
function initOverView(overview) {

    // document.querySelector('[name=salary]').innerHTML = overview['salary']
    // document.querySelector('[name=age]').innerHTML = overview['age']
    // document.querySelector('[name=student_count]').innerHTML = overview['student_count']
    // document.querySelector('[name=group_count]').innerHTML = overview['group_count']

    // 遍历对象
    for (const key in overview) {
        document.querySelector(`[name=${key}]`).innerHTML = overview[key]
    }

}

// 渲染折线图 - 全学科薪资走势
function initLine(year) {
    // 1. 实例化echarts
    const myChart = echarts.init(document.querySelector('#line'))
    // 2. 准备配置
    const option = {
        title: {
            text: '2021全学科薪资走势',
            left: 20,
            top: 20
        },
        tooltip: {
            trigger: 'axis'
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: year.map(v => v.month),
            axisLine: {
                lineStyle: {
                    color: '#ccc',
                    type: 'dashed' // 设置线条类型为虚线  
                }
            }
        },
        yAxis: {
            type: 'value',
            splitLine: {
                lineStyle: {
                    color: '#ccc',
                    type: 'dashed' // 设置线条类型为虚线  
                }
            }
        },
        series: [
            {
                data: year.map(v => v.salary),
                type: 'line',
                smooth: true,
                symbolSize: 12,
                lineStyle: {
                    color: '#499eee',
                    width: 8
                },
                areaStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        {
                            offset: 0,
                            color: '#9bcaf6'
                        },
                        {
                            offset: 1,
                            color: '#fff'
                        }
                    ])
                }
            }
        ]
    };
    // 3. 让echarts使用这个配置
    myChart.setOption(option)
}

// 渲染饼图 - 班级薪资分布
function initPie(list) {

    const myChart = echarts.init(document.querySelector('#salary'))
    const option = {
        title: {
            text: '班级薪资分布',
            left: 20,
            top: 20,
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            bottom: '5%',
            left: 'center'
        },
        color: ['#fda224', '#5097ff', '#3abcfa', '#34d39a'],
        series: [
            {
                name: '班级薪资分布',
                type: 'pie',
                radius: ['55%', '70%'],
                center: ['50%', '45%'],
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },
                labelLine: {
                    show: false
                },
                data: list.map(v => {
                    return { value: v.g_count + v.b_count, name: v.label }
                })
            }
        ]
    };

    myChart.setOption(option)
}


// 渲染柱状图 - 班级每组薪资
function initBar(list) {

    const myChart = echarts.init(document.querySelector('#lines'))


    const option = {

        tooltip: {
            trigger: 'axis'
        },
        calculable: true,
        xAxis:
        {
            type: 'category',
            // prettier-ignore
            data: list[1].map(v => v.name),

            axisLine: {
                lineStyle: {
                    color: '#ccc',
                    type: 'dashed'
                }
            }
        },
        yAxis: [
            {
                type: 'value',
                splitLine: {
                    lineStyle: {
                        type: 'dashed'
                    }
                }
            }
        ],
        series: [
            {
                name: '期望薪资',
                type: 'bar',
                data: list[1].map(v => v.hope_salary),
                itemStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: '#188df0' },
                        { offset: 1, color: '#83bff6' }
                    ])
                },
            },
            {
                name: '就业薪资',
                type: 'bar',
                data: list[1].map(v => v.salary),
                itemStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: '#49d7a4' },
                        { offset: 1, color: '#d0f4e7' }
                    ])
                },
            }
        ]
    };

    myChart.setOption(option)

    // 给所有的按钮加点击事件
    document.querySelector('#btns').addEventListener('click', function (e) {
        if (e.target.tagName === 'BUTTON') {
            // 排他
            document.querySelector('.btn-blue').classList.remove('btn-blue')
            // 点谁谁高亮
            e.target.classList.add('btn-blue')
            // 点谁拿到谁的下标
            const idx = e.target.innerHTML

            // 修改配置
            option.xAxis.data = list[idx].map(v => v.name)
            option.series[0].data = list[idx].map(v => v.hope_salary)
            option.series[1].data = list[idx].map(v => v.salary)

            // 配置改了一定要调用setOption才能重新生效
            myChart.setOption(option)
        }
    })
}

// 渲染籍贯
function initMapChart(provinceData) {

    const myEchart = echarts.init(document.querySelector('#map'))
    const dataList = [
        { name: '南海诸岛', value: 0 },
        { name: '北京', value: 0 },
        { name: '天津', value: 0 },
        { name: '上海', value: 0 },
        { name: '重庆', value: 0 },
        { name: '河北', value: 0 },
        { name: '河南', value: 0 },
        { name: '云南', value: 0 },
        { name: '辽宁', value: 0 },
        { name: '黑龙江', value: 0 },
        { name: '湖南', value: 0 },
        { name: '安徽', value: 0 },
        { name: '山东', value: 0 },
        { name: '新疆', value: 0 },
        { name: '江苏', value: 0 },
        { name: '浙江', value: 0 },
        { name: '江西', value: 0 },
        { name: '湖北', value: 0 },
        { name: '广西', value: 0 },
        { name: '甘肃', value: 0 },
        { name: '山西', value: 0 },
        { name: '内蒙古', value: 0 },
        { name: '陕西', value: 0 },
        { name: '吉林', value: 0 },
        { name: '福建', value: 0 },
        { name: '贵州', value: 0 },
        { name: '广东', value: 0 },
        { name: '青海', value: 0 },
        { name: '西藏', value: 0 },
        { name: '四川', value: 0 },
        { name: '宁夏', value: 0 },
        { name: '海南', value: 0 },
        { name: '台湾', value: 0 },
        { name: '香港', value: 0 },
        { name: '澳门', value: 0 },
    ]

    // 对dataList进行遍历
    dataList.forEach(v1 => {

        const res = provinceData.find(v2 => v2.name.indexOf(v1.name) !== -1)
        if (res) {
            // 找到了
            v1.value = res.value
        }
    })

    let option = {
        title: {
            text: '籍贯分布',
            top: 10,
            left: 10,
            textStyle: {
                fontSize: 16,
            },
        },
        tooltip: {
            trigger: 'item',
            formatter: '{b}: {c} 位学员',
            borderColor: 'transparent',
            backgroundColor: 'rgba(0,0,0,0.5)',
            textStyle: {
                color: '#fff',
            },
        },
        visualMap: {
            min: 0,
            max: 6,
            left: 'left',
            bottom: '20',
            text: ['6', '0'],
            inRange: {
                color: ['#ffffff', '#0075F0'],
            },
            show: true,
            left: 40,
        },
        geo: {
            map: 'china',
            roam: false,
            zoom: 1.0,
            label: {
                normal: {
                    show: true,
                    fontSize: '10',
                    color: 'rgba(0,0,0,0.7)',
                },
            },
            itemStyle: {
                normal: {
                    borderColor: 'rgba(0, 0, 0, 0.2)',
                    color: '#e0ffff',
                },
                emphasis: {
                    areaColor: '#34D39A',
                    shadowOffsetX: 0,
                    shadowOffsetY: 0,
                    shadowBlur: 20,
                    borderWidth: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)',
                },
            },
        },
        series: [
            {
                name: '籍贯分布',
                type: 'map',
                geoIndex: 0,
                data: dataList,
            },
        ],
    }
    myEchart.setOption(option)
}


// 渲染双饼图 - 男女薪资分布
function initTwoPie(salaryData) {

    const myChart = echarts.init(document.querySelector('#gender'))


    const option = {
        title: [
            {
                text: '男女薪资分布',
                left: 10,
                top: 20,
                textStyle: {
                    fontSize: 16
                }
            },
            {
                text: '男生',
                left: 'center',
                top: '52%',
                textStyle: {
                    fontSize: 12
                }
            },
            {
                text: '女生',
                left: 'center',
                bottom: '4%',
                textStyle: {
                    fontSize: 12
                }
            },
        ],
        tooltip: {
            trigger: 'item'
        },
        color: ['#fda224', '#5097ff', '#3abcfa', '#34d39a'],
        series: [
            {
                name: '男生薪资分布',
                type: 'pie',
                radius: ['25%', '35%'],
                center: ['50%', '35%'],
                data: salaryData.map(v => ( { name: v.label, value: v.b_count } )),
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            },
            {
                name: '女生薪资分布',
                type: 'pie',
                radius: ['25%', '35%'],
                center: ['50%', '75%'],
                data: salaryData.map(v => ( { name: v.label, value: v.g_count } )),
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };

    myChart.setOption(option)
}

loadData()